package com.kzj.global.exceptionHandler;

import com.kzj.global.constant.GlobalConstant;
import com.kzj.global.exception.MyException;
import com.kzj.global.re.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;

@ControllerAdvice(annotations = {Controller.class, RestController.class})
@Component
@ResponseBody
@Slf4j
public class GlobalExceptionHandler {
    @ExceptionHandler(MyException.class)
    public Result myExceptionHandler(MyException exception, HttpServletResponse httpServletResponse)
    {
        log.error(exception.getMessage());
        httpServletResponse.setStatus(GlobalConstant.ERROR_STATUS);
        return Result.error("myExceptionHandler: "+exception.getMessage());
    }
//    @ExceptionHandler(Exception.class)
//    Result ExceptionHandler(Exception exception,HttpServletResponse httpServletResponse)
//    {
//        log.error(exception.getMessage());
//        httpServletResponse.setStatus(GlobalConstant.ERROR_STATUS);
//        return Result.error("ExceptionHandler:"+exception.getMessage());
//    }
}
